package com.itqf.controller;

import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.bean.DtsStorage;
import com.itqf.dto.DtsSearchDto;
import com.itqf.service.DtsStorageService;
import com.itqf.util.ResponseUtil;
import com.itqf.utils.AliyunOSSUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * projectName: shop_manage
 *
 * @author: MaTianWei
 * time: 2021/7/16 20:10
 * description:
 */
@RestController
@CrossOrigin("*")
@RequestMapping("admin/storage")
public class AdminStorageController {

    //服务器地址
    @Value("${oss.endpoint}")
    private String endpoint;
    //用户访问的id
    @Value("${oss.accessKeyId}")
    private String accessKeyId;
    //用户访问的密码
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;
    //桶名字
    @Value("${oss.bucketName}")
    private String bucketName;


    @Autowired
    private DtsStorageService dtsStorageService;
    @RequiresPermissions("admin:storage:create")
    @RequiresPermissionsDesc(menu = {"系统管理","对象管理"},button = "对象添加")
    @PostMapping("create")
    public Object create(MultipartFile file) throws Exception {
        //1·存到oss里边
        String fileName = file.getOriginalFilename();//获取文件名
        String key = UUID.randomUUID().toString().replaceAll("-", "")+fileName;
        String url =  AliyunOSSUtil.uploadImage(endpoint,accessKeyId,accessKeySecret,bucketName,key,file.getBytes());
        //存到数据库
        DtsStorage dtsStorage = new DtsStorage();
        dtsStorage.setKey(key);
        dtsStorage.setName(fileName);
        dtsStorage.setType(fileName.substring(fileName.lastIndexOf(".")));
        dtsStorage.setSize((int)file.getSize());
        dtsStorage.setUrl(url);
        dtsStorage.setAddTime(new Date());
        dtsStorage.setDeleted(false);
        //2·把记录保存到数据库
        dtsStorageService.save(dtsStorage);
        return ResponseUtil.ok(dtsStorage);
    }





    //查看
    @RequiresPermissions("admin:storage:list")
    @RequiresPermissionsDesc(menu = {"系统管理","对象管理"},button = "对象查询")
    @GetMapping("list")
    public Object list(DtsSearchDto dto){
        Map data = dtsStorageService.query(dto);
        return ResponseUtil.ok(data);
    }



    //更新admin/storage/update
    @RequiresPermissions("admin:storage:update")
    @RequiresPermissionsDesc(menu = {"系统管理","对象管理"},button = "对象更新")
    @PostMapping("update")
    public Object update(@RequestBody DtsStorage dtsStorage){
        int rows = dtsStorageService.update(dtsStorage);
        return ResponseUtil.ok();
    }




    //删除/admin/storage/delete
    @RequiresPermissions("admin:storage:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","对象管理"},button = "对象删除")
    @PostMapping("delete")
    public Object delete(@RequestBody DtsStorage dtsStorage){
       int rows = dtsStorageService.delete(dtsStorage.getId());
        return ResponseUtil.ok();
    }

}
